//
//  GPAlterAliasController.m
//  Keping
//
//  Created by 23 on 2017/12/9.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPAlterAliasController.h"
#import "GPContactRequest.h"

@interface GPAlterAliasController ()

@property (weak, nonatomic) IBOutlet UITextField *aliasTextField;


@end

@implementation GPAlterAliasController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.aliasTextField.placeholder = self.placeholder;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(saveAlias)];
    
}


#pragma mark - action 
- (void)saveAlias
{
    [self.view endEditing:YES];
    if ([self.aliasTextField.text isNotBlank]) {
        
        WEAKSELF
        [self showLoading];
        NSString *url = [kIMAlterUserInfoURL stringByReplacingOccurrencesOfString:@"{userId}" withString:self.userId];
        GPContactRequest *request = [[GPContactRequest alloc] initWithURL:url];
        request.isPost = YES;
        request.params = @{@"remarks":self.aliasTextField.text};
        [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
            [self hideHUD];
            NSDictionary *response = request.responseJSONObject;
            
            if ([response[@"statusCode"] integerValue] != 200) {
                
                [self showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
                
                
                return;
            }
            
            [self showSuccess:@"success"];
            
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
            
            
        } failure:^(__kindof KBaseRequest * _Nonnull request) {
            
            [self showErrorText:@"failed"];
            
        }];
       

    }
    
    
}


@end
